
/**
 * 解决Vue Template模板中无法使用可选链的问题
 * @param obj
 * @param rest
 * @returns {*}
 */
// import moment from "moment";
export const optionalChaining = (obj, ...rest) => {
    let tmp = obj;
    for (let key in rest) {
        let name = rest[key];
        tmp = tmp?.[name];
    }
    return tmp || void 0;
};
/**获取两个时间之间的时间列表 */
export const getTimeList = function (startTime, endTime, step, stepType) {
    const timeList = [];
    // timeList.push(startTime.add(this.step, this.stepType));
    while (startTime <= endTime) {
        timeList.push(startTime);
        const newStart = startTime.add(step, stepType);
        startTime = newStart;
    }
    return timeList;
}
export const dowloadBlob = (fileNmae, file) => {
    let a = document.createElement("a");
    document.body.appendChild(a);
    a.style = "display: none";
    const url = window.URL.createObjectURL(file);
    a.href = url;
    a.download = `${fileNmae}`;
    a.click();
    a.remove();
    window.URL.revokeObjectURL(url);
}
export const dowloadImage = (fileNmae, imgsrc) => {
    let image = new Image();
    // 解决跨域 Canvas 污染问题
    image.setAttribute("crossOrigin", "anonymous");
    image.onload = function () {
        let canvas = document.createElement("canvas");
        canvas.width = image.width;
        canvas.height = image.height;
        let context = canvas.getContext("2d");
        context.drawImage(image, 0, 0, image.width, image.height);
        let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
        let a = document.createElement("a"); // 生成一个a元素
        let event = new MouseEvent("click"); // 创建一个单击事件
        a.download = fileNmae || "photo"; // 设置图片名称
        a.href = url; // 将生成的URL设置为a.href属性
        a.dispatchEvent(event); // 触发a的单击事件
    };
    image.src = imgsrc;
    image.setAttribute("crossOrigin", "anonymous");

}
export const isEmpty = (val) => {
    // null or undefined
    if (val == null) return true;
    if (typeof val === 'boolean') return false;
    if (typeof val === 'number') return !val;
    if (val instanceof Error) return val.message === '';
    switch (Object.prototype.toString.call(val)) {
        // String or Array
        case '[object String]':
        case '[object Array]':
            return !val.length;

        // Map or Set or File
        case '[object File]':
        case '[object Map]':
        case '[object Set]': {
            return !val.size;
        }
        // Plain Object
        case '[object Object]': {
            return !Object.keys(val).length;
        }
    }

    return false;
}

export function blobToDataURI(blob) {
    return new Promise(function (res) {
        var reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onload = function (e) {
            res(e.target.result)
        };
    })
}

export const install = function (Vue) {
    Vue.prototype.$$ = optionalChaining
    Vue.prototype.$isEmpty = isEmpty
    Vue.prototype.$tool = Vue.prototype.$util = {
        optionalChaining,
        dowloadBlob,
        dowloadImage,
        isEmpty,
        blobToDataURI,
        getTimeList
    }
}
